package com.exchange.controller;

import com.exchange.entity.DeliveryTracking;
import com.exchange.service.DeliveryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/delivery")
@CrossOrigin(origins = "*")
@PreAuthorize("hasRole('MERCHANT') or hasRole('ADMIN')")
public class DeliveryController {
    
    @Autowired
    private DeliveryService deliveryService;
    
    // Get delivery history for an order
    @GetMapping("/order/{orderId}")
    public ResponseEntity<List<DeliveryTracking>> getDeliveryHistory(@PathVariable Long orderId) {
        List<DeliveryTracking> history = deliveryService.getDeliveryHistory(orderId);
        return ResponseEntity.ok(history);
    }
    
    // Add a delivery update (for merchants to update tracking information)
    @PostMapping
    public ResponseEntity<DeliveryTracking> addDeliveryUpdate(@RequestBody DeliveryUpdateRequest request) {
        DeliveryTracking deliveryUpdate = deliveryService.addDeliveryUpdate(
            request.getOrderId(),
            request.getTrackingNumber(),
            request.getDeliveryCompany(),
            request.getStatus(),
            request.getLocation(),
            request.getDetails()
        );
        return ResponseEntity.ok(deliveryUpdate);
    }
    
    // Update delivery status only
    @PutMapping("/{orderId}/status")
    public ResponseEntity<DeliveryTracking> updateDeliveryStatus(@PathVariable Long orderId, 
                                                                 @RequestBody StatusUpdateRequest request) {
        DeliveryTracking updatedDelivery = deliveryService.updateOrderDeliveryStatus(orderId, request.getStatus());
        return ResponseEntity.ok(updatedDelivery);
    }
    
    // Request classes
    public static class DeliveryUpdateRequest {
        private Long orderId;
        private String trackingNumber;
        private String deliveryCompany;
        private String status;
        private String location;
        private String details;
        
        // Getters and setters
        public Long getOrderId() { return orderId; }
        public void setOrderId(Long orderId) { this.orderId = orderId; }
        
        public String getTrackingNumber() { return trackingNumber; }
        public void setTrackingNumber(String trackingNumber) { this.trackingNumber = trackingNumber; }
        
        public String getDeliveryCompany() { return deliveryCompany; }
        public void setDeliveryCompany(String deliveryCompany) { this.deliveryCompany = deliveryCompany; }
        
        public String getStatus() { return status; }
        public void setStatus(String status) { this.status = status; }
        
        public String getLocation() { return location; }
        public void setLocation(String location) { this.location = location; }
        
        public String getDetails() { return details; }
        public void setDetails(String details) { this.details = details; }
    }
    
    public static class StatusUpdateRequest {
        private String status;
        
        public String getStatus() { return status; }
        public void setStatus(String status) { this.status = status; }
    }
}